﻿using System;
using System.Text.RegularExpressions;
namespace Validate.Net
{
    /// <summary>
    /// 正则校验基类
    /// </summary>
    public  class ValidateRegularAttribute : AbstractValidateAttribute
    {
        private string regularStr = null;
        private string errMsg = null;

        public ValidateRegularAttribute(string regular)
        {
            this.regularStr = regular;
        }

        public ValidateRegularAttribute(string regular, string errMsg)
        {
            this.errMsg = errMsg;
            this.regularStr = regular;
        }

        public override bool Validate(object value, out string errMsg)
        {
            errMsg = this.errMsg;
            return !string.IsNullOrWhiteSpace(value?.ToString())
                && Regex.IsMatch(value.ToString(), regularStr);
        }
    }
}
